<?php
/**
 * dbTable 基础类
 * @author Administrator
 *
 */
class Application_Model_DbTable_Base extends Zend_Db_Table_Abstract
{

    /**
	 * 表前缀
     */
	protected $_prefix;
	protected $_name = 'base';
    protected $_user_id = 0;
    private $_APP_CONF_SESSION_KEY = "APP_CONF";
    
    /**
     * 储存用户信息的session
     * @var Zend_Session_Namespace
     */
    protected $_S;
    
    /**
     * 字段过滤器
     */
    protected $_columns_filter;

    /**
     * 
     * Enter description here ...
     * @param unknown_type $config
     */
    public function __construct($config = NULL) {
    	//从配置中读取前缀
    	$appconf = new Zend_Session_Namespace($this->_APP_CONF_SESSION_KEY);
    	$this->_prefix = $appconf->table_prefix?$appconf->table_prefix:"";
    	
    	$this->_name = "{$this->_prefix}{$this->_name}";
    	
    	//从session中读取用户信息
    	$this->_S = new Zend_Session_Namespace(Application_Model_Login::LOGIN_SESSION_KEY);
		$this->_user_id = $this->_S->id?$this->_S->id:0;
    	parent::__construct($config);
    }
    
    public function getTableName(){
    	return $this->_name;
    }

    public function insertReturnData(array $data){
    	$id = parent::insert($data);
    	if ($id) {
    		$data['id'] = $id;
    		return $data;
    	} else {
    		return false;
    	}
    }

     public function fetchOne($sql, $bind = array()){
   		return $this->_db->fetchOne($sql,$bind);
     }

	public function getParentSelect ($withFromPart = self::SELECT_WITHOUT_FROM_PART, $filter = null)
    {
        return parent::select($withFromPart);
    }
    
    /**
     * Fetches all rows by page.
     *
     * 返回分页数据集
     *
     * @param objct $pageM
     * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
     * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
     * @param int                               $count  OPTIONAL An SQL LIMIT count.
     * @param int                               $offset OPTIONAL An SQL LIMIT offset.
     * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
     */
    public function jqfetchAll($pageM=null,$where = null, $order = null, $count = null, $offset = null,$filter=null)
    {
        if ($pageM == NULL) {
            return $this->fetchAll($where, $order, $count, $offset)->toArray();
        }
        
        /*如果直接继承自DBTABLE SELECT*/
        if ($where instanceof Zend_Db_Table_Select) {
        	
            $select = $where;            
            /*如果直接继承自DB SELECT*/
        } else if ($where instanceof Zend_Db_Select) {
            $select = $where;
            
            /*如果什么都没有继承*/
        } else {
            if ($filter) {
                $select = $this->select(null, $filter);
            } else {
                $select = $this->select();
            }
        }
        
    	//分页数据
        if ($pageM->qtype && ($pageM->query != "") && preg_match("/^[\w.]+$/", $pageM->qtype)) {
            $select->where("$pageM->qtype like ?", "%$pageM->query%");
        }
        if ($pageM->sortname && preg_match("/^[\w.]+$/", $pageM->sortname)) {
            if ($pageM->sortorder) {
                $select->order("$pageM->sortname  $pageM->sortorder");
            } else {
                $select->order($pageM->sortname);
            }
        }
        
        //参数数据
        if (!($where instanceof Zend_Db_Select) && $where) {
            $select->where($where);
        }
        if ($order !== null) {
            $select->order($order);
        }
        if ($count !== null || $offset !== null) {
            $select->limit($count, $offset);
        }
        
        //分页器
        /*if ($select instanceof Zend_Db_Select) {*/
            $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        /*} else {
            $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
        }*/
        
        $paginator->setCurrentPageNumber($pageM->page);//设置当前页数
        $paginator->setItemCountPerPage($pageM->rp); //设置每页显示条数
        
        $data = array(
        	'data' => $paginator->getCurrentItems(), 
        	'total' => $paginator->getTotalItemCount(), 
        	'page' => $paginator->getCurrentPageNumber()
        );
        return $data;
        
        /*$data = array('table' => $this, 'data' => $rows, 
        'readOnly' => $select->isReadOnly(), 'rowClass' => $this->getRowClass(), 
        'stored' => true);
        $rowsetClass = $this->getRowsetClass();
        if (! class_exists($rowsetClass)) {
            require_once 'Zend/Loader.php';
            Zend_Loader::loadClass($rowsetClass);
        }
        return new $rowsetClass($data);*/
    }
    
    /**
     * 删除临时数据
     * @return void
     */
    public function deleteTempItem(){
    	try{
			$this->delete( array("temp_state" => "1" ));
		}catch (Exception $e){
			
		}
    }
}


